// Package codes
// @File: code.go
// @Description: 状态码定义
// @Author: YaoRuiQi
// @Date: 2025/8/23 02:51

package codes

import (
	"GinStandardTemplate/internal/pkg/contextUtils"
	i18nUtils "GinStandardTemplate/utils/i18n"
)

// CodeInfo
// @Description: 状态码信息
type CodeInfo struct {
	code      int    // 业务 code，在接口返回数据中返回给前端
	messageID string // i18n 消息ID
}

func (c CodeInfo) Code() int {
	return c.code
}

func (c CodeInfo) MessageID() string {
	return c.messageID
}

// Message
//
//	@Description: 获取国际化处理后消息
func (c CodeInfo) Message(ctx *contextUtils.RequestContext, templateData map[string]interface{}) string {
	return i18nUtils.GetMessage(ctx, c.messageID, templateData)
}

var (
	// 通用业务

	Success       = CodeInfo{code: 0, messageID: "success"}               // 成功
	InternalError = CodeInfo{code: 500, messageID: "error.internal"}      // 系统异常
	InvalidParam  = CodeInfo{code: 1001, messageID: "error.invalidParam"} // 参数错误
	InvalidURL    = CodeInfo{code: 1002, messageID: "error.invalidURL"}   // 请求URL不正确，不是以/api或/ws开头

	// 用户相关业务

	UserUnauthorized = CodeInfo{code: 401, messageID: "error.user.unauthorized"}     // 用户未登录或 token 失效
	UserForbidden    = CodeInfo{code: 403, messageID: "error.user.forbidden"}        // 用户已登录，但无权限访问
	UserDisabled     = CodeInfo{code: 2001, messageID: "error.user.disabled"}        // 用户被禁用
	UserNotFound     = CodeInfo{code: 2002, messageID: "error.user.notFound"}        // 用户不存在
	PasswordError    = CodeInfo{code: 2003, messageID: "error.user.passwordError"}   // 密码错误
	AccountNotExist  = CodeInfo{code: 2004, messageID: "error.user.accountNotExist"} // 账号不存在

	// 业务操作

	CreateFailed       = CodeInfo{code: 3001, messageID: "error.operation.createFailed"}       // 创建失败
	UpdateFailed       = CodeInfo{code: 3002, messageID: "error.operation.updateFailed"}       // 更新失败
	DeleteFailed       = CodeInfo{code: 3003, messageID: "error.operation.deleteFailed"}       // 删除失败
	SearchFailed       = CodeInfo{code: 3004, messageID: "error.operation.searchFailed"}       // 查询失败
	NotFound           = CodeInfo{code: 3005, messageID: "error.operation.notFound"}           // 未找到
	InvalidState       = CodeInfo{code: 3006, messageID: "error.operation.invalidState"}       // 请求的操作在当前资源状态下不允许
	DuplicateOperation = CodeInfo{code: 3007, messageID: "error.operation.duplicateOperation"} // 重复操作，例如状态已经是关闭，客户端再次请求关闭；或幂等性检查失败时返回
)
